package com.scau.hxf;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ImageServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public ImageServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		BufferedImage bImage=new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB);
		Graphics g=bImage.getGraphics();
		Color c=new Color(200,150,255);//用于设置背景颜色
		g.setColor(c);
		g.fillRect(0, 0, 68, 22);//画出矩形背景
		
		char[] ch="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".toCharArray();//验证码所有元素
		Random random=new Random();
		int l=ch.length,index;
		StringBuffer sBuffer=new StringBuffer();
		for (int i = 0; i < 4; i++) {
			index=random.nextInt(l);//随机产生第几个元素的位置
			g.setColor(new Color(random.nextInt(88), random.nextInt(188), random.nextInt(255)));
			g.drawString(ch[index]+"", i*15+3, 18);//将元素描绘在bImage上
			sBuffer.append(ch[index]);
		}
		request.getSession().setAttribute("picCode", sBuffer.toString());//存储验证码
		ImageIO.write(bImage, "JPG", response.getOutputStream());//输出验证码
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
